package com.withoutwords.ui;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;

import com.withoutwords.R;
import com.withoutwords.engine.BitmapCache;
import com.withoutwords.engine.Common;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.Round;
import com.withoutwords.engine.RoundMediaTime;
import com.withoutwords.engine.RoundMediaType;
import com.withoutwords.ui.custom.ArrayWheelAdapter;
import com.withoutwords.ui.custom.ImageWheelAdapter;
import com.withoutwords.ui.custom.MyCustomButton;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class StartRoundActivity extends BaseActivity implements OnClickListener {

	Round round;
	int[][] icons = new int[][] {
			{ R.drawable.film_old, R.drawable.play_old, R.drawable.song_old,
					R.drawable.series_old },
			{ R.drawable.film_new, R.drawable.play_new, R.drawable.song_new,
					R.drawable.series_new },
			{ R.drawable.film_current, R.drawable.play_current,
					R.drawable.song_current, R.drawable.series_current } };
	// Scrolling flag
	private boolean scrolling = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startround);

		initViews();

		round = new Round();

		playSound(soundResId);
	}

	@Override
	protected void setBitmaps() {
		// TODO Auto-generated method stub
		super.setBitmaps();
		setLayoutBitmap(R.id.lyRoundInfo, R.drawable.media_info);

	}

	@Override
	protected void unsetBitmaps() {
		// TODO Auto-generated method stub
		super.unsetBitmaps();
		unsetLayoutBitmaps(R.id.lyRoundInfo);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			
			GameController.getInstance().leaveGame(GameController.getInstance().getCurrentGame().getGameId());
		}
		return super.onKeyDown(keyCode, event);
	}

	void initViews() {

		MyCustomButton btn = (MyCustomButton) findViewById(R.id.btnStartRound);
		btn.setOnClickListener(this);

		final WheelView wheelType = (WheelView) findViewById(R.id.wheelViewType);
		ImageWheelAdapter imgAdpt = new ImageWheelAdapter(this);
		imgAdpt.setItems(icons[1]);

		wheelType.setViewAdapter(imgAdpt);
		wheelType.setCurrentItem(1);

		/*
		 * wheel.addChangingListener(changedListener);
		 * wheel.addScrollingListener(scrolledListener);
		 */
		wheelType.setCyclic(true);
		// wheel.setEnabled(false);

		final WheelView wheelTime = (WheelView) findViewById(R.id.wheelViewTime);
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(
				this,
				new String[] { getString(R.string.old),
						getString(R.string.middle), getString(R.string.recent) });
		adapter.setTextSize(40);
		wheelTime.setViewAdapter(adapter);
		wheelTime.setCurrentItem(1);
		wheelTime.setCyclic(true);

		wheelTime.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {

					updateImageWheel(wheelType, newValue);
				}
			}
		});

		wheelTime.addScrollingListener(new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
				updateImageWheel(wheelType, wheelTime.getCurrentItem());
				// updateCities(wheelType, cities, country.getCurrentItem());
			}
		});
	}

	@Override
	protected void releaseBitmaps() {
		// TODO Auto-generated method stub
		super.releaseBitmaps();

		BitmapCache.getCacheInstance().deleteDrawable(R.drawable.media_info);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnStartRound: {
			EditText txt = (EditText) findViewById(R.id.editMediaName);
			String mediaName = txt.getText().toString();
			if (!mediaName.trim().equalsIgnoreCase("")) {
				WheelView wheel = (WheelView) findViewById(R.id.wheelViewTime);
				round.setMediaTime(RoundMediaTime.values()[wheel
						.getCurrentItem()]);

				wheel = (WheelView) findViewById(R.id.wheelViewType);
				round.setMediaType(RoundMediaType.values()[wheel
						.getCurrentItem()]);

				round.setMediaName(mediaName);

				Common.hideKeyboard(this, txt);
				GameController.getInstance().createRound(round);
				v.setEnabled(false);
			} else {
				Toast.makeText(this,
						getResources().getString(R.string.media_name_hint),
						Toast.LENGTH_LONG).show();

			}
			// releaseBitmaps();
			// finish();
			break;
		}
		// case R
		}

	}

	/**
	 * Updates the city wheel
	 */
	private void updateImageWheel(WheelView imageWheel, int index) {
		ImageWheelAdapter imgAdpt = new ImageWheelAdapter(this);
		imgAdpt.setItems(icons[index]);
		// adapter.setTextSize(18);
		imageWheel.setViewAdapter(imgAdpt);
		// imageWheel.setCurrentItem(icons[index].length / 2);
	}

}
